//
//  XFNewFeatureViewController.swift
//  mxfweibo
//
//  Created by apple1 on 15/12/17.
//  Copyright © 2015年 莫祥富. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class XFNewFeatureViewController: UICollectionViewController {

    private let layout = UICollectionViewFlowLayout()
    
    init(){
    
        super.init(collectionViewLayout:layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private let itemCount = 4
    
    override func viewDidLoad() {
        super.viewDidLoad()

        prepareCollectionView()

    }

    
    func prepareCollectionView(){
        
        self.collectionView!.registerClass(XFNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        layout.itemSize  = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        
    }
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return itemCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! XFNewFeatureCell
        cell.index = indexPath.item
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        
        if visibleIndexPath.item == itemCount - 1 {
            
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath) as! XFNewFeatureCell
            
            cell.startButtonAnimation()
        }
    }
    
 
}

class XFNewFeatureCell:UICollectionViewCell {

    var index = 0 {
    
        didSet {
            bgimgView.image = UIImage(named: "new_feature_\(index + 1)")
            startBtn.hidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    //MARK: - 加载开始按钮的动画
    func startButtonAnimation(){
        startBtn.hidden = false
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)

       UIView.animateWithDuration(0.8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
        self.startBtn.transform = CGAffineTransformIdentity
        
        }) { (_) -> Void in
            
        }
    }
    
    //MARK: - 准备UI
    private  func prepareUI(){
    
        contentView.addSubview(bgimgView)
        contentView.addSubview(startBtn)
        
//        bgimgView.translatesAutoresizingMaskIntoConstraints = false
//        startBtn.translatesAutoresizingMaskIntoConstraints = false
//        
//        contentView.addConstraint(NSLayoutConstraint(item: bgimgView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
//        contentView.addConstraint(NSLayoutConstraint(item: bgimgView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
//        contentView.addConstraint(NSLayoutConstraint(item: bgimgView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        contentView.addConstraint(NSLayoutConstraint(item: bgimgView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
//        
//        contentView.addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        contentView.addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
        bgimgView.ff_Fill(contentView)
        
        startBtn.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
    }
    
    //点击按钮
    func stratBtnClick() {
        
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
    }

    //MARK: － 数据懒加载
    private lazy var bgimgView : UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startBtn:UIButton = {
       let button = UIButton()
    
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        button.addTarget(self, action: "stratBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
   
    
    
}





